package com.xyh.student.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xyh.student.dao.PaperDao;
import com.xyh.student.dao.QuestionDao;
import com.xyh.student.entity.Paper;
import com.xyh.student.entity.Question;
import com.xyh.student.entity.ResponseData;
import com.xyh.student.service.PaperService;
import com.xyh.student.utils.DeleteNull;

@Service("PaperService")
public class PaperServiceImpl implements PaperService {

    @Autowired
    private QuestionDao questionDao;

    @Autowired
    private PaperDao paperDao;

    @Override
    public ResponseData newPaperByAuto(int paperId) { // n题目数量
        // 一键生成默认生成所有题库中的各种题型，每样五个！！
        try {
            List<Question> single = findQuestion(1, 5);
            List<Question> multiple = findQuestion(2, 5);
            List<Question> estimate = findQuestion(3, 5);
            List<Question> gap = findQuestion(4, 5);
            List<Question> essay = findQuestion(5, 5);
            List<Question> questions  = new ArrayList<>();
            questions.addAll(single);
            questions.addAll(multiple);
            questions.addAll(estimate);
            questions.addAll(gap);
            questions.addAll(essay);
            List<Paper> papers = new ArrayList<>();
            //将试卷题目保存到数据库中
            for(Question question:questions){
                Paper paper = new Paper();
                paper.setId(paperId);
                paper.setQid(question.getQid());
                papers.add(paper);
            }
            paperDao.insertPaper(papers);
            for (int i = 0; i < single.size(); i++) {
                String[] options = single.get(i).getOption().toString().split(",");
                String[] newoptions = DeleteNull.deleteNull(options);
                // 设置题目选项
                single.get(i).setOp(newoptions);
            }
            for (int i = 0; i < multiple.size(); i++) {
                String[] options = multiple.get(i).getOption().toString().split(",");
                String[] newoptions = DeleteNull.deleteNull(options);
                multiple.get(i).setOp(newoptions);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("single", single);
            map.put("multip", multiple);
            map.put("estimate", estimate);
            map.put("gap", gap);
            map.put("essay", essay);
            return new ResponseData("0", "请求成功", map);
        } catch (Exception e) {
            System.out.println(e);
            return new ResponseData("888", "请求失败");
        }
    }

    @Override
    public ResponseData newPaper(int paperId,int[] libraryIds, int[] nums) {
        try {
            // 选择某几个题库的某些题目
            // nums题目的数量
            List<Question> single = findQuestionByHand(libraryIds, nums[0], 1);
            List<Question> multiple = findQuestionByHand(libraryIds, nums[1], 2);
            List<Question> estimate = findQuestionByHand(libraryIds, nums[2], 3);
            List<Question> gap = findQuestionByHand(libraryIds, nums[3], 4);
            List<Question> essay = findQuestionByHand(libraryIds, nums[4], 5);
            List<Question> questions  = new ArrayList<>();
            questions.addAll(single);
            questions.addAll(multiple);
            questions.addAll(estimate);
            questions.addAll(gap);
            questions.addAll(essay);
            List<Paper> papers = new ArrayList<>();
            //将试卷题目保存到数据库中
            for(Question question:questions){
                Paper paper = new Paper();
                paper.setId(paperId);
                paper.setQid(question.getQid());
                papers.add(paper);
            }
            paperDao.insertPaper(papers);
            for (int i = 0; i < single.size(); i++) {
                String[] options = single.get(i).getOption().toString().split(",");
                String[] newoptions = DeleteNull.deleteNull(options);
                // 设置题目选项
                single.get(i).setOp(newoptions);
            }
            for (int i = 0; i < multiple.size(); i++) {
                String[] options = multiple.get(i).getOption().toString().split(",");
                String[] newoptions = DeleteNull.deleteNull(options);
                multiple.get(i).setOp(newoptions);
            }
            // System.out.println(multiple.toString());
            // System.out.println(single.toString());
            Map<String, Object> map = new HashMap<>();
            map.put("single", single);
            map.put("multip", multiple);
            map.put("estimate", estimate);
            map.put("gap", gap);
            map.put("essay", essay);
            return new ResponseData("0", "请求成功", map);
        } catch (Exception e) {
            System.out.println(e);
            return new ResponseData("888", "请求失败");
        }
    }

    /**
     * 
     * @param type 传入题目类型
     * @param n    传入题目数量
     */
    public List<Question> findQuestion(int type, int n) {
        try {
            List<Question> questions = questionDao.findQuestionByType(type);
            List<Question> newQuestion = new ArrayList<Question>();
            Map<Integer, String> map = new HashMap<Integer, String>();
            if (questions.size() <= n) {
                return questions;
            } else {
                while (map.size() < n) {
                    int random = (int) (Math.random() * questions.size());
                    // 判断Key是否存在
                    if (!map.containsKey(random)) {
                        map.put(random, "");
                        newQuestion.add(questions.get(random));
                    }
                }
            }
            return newQuestion;
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    /**
     * 
     * @param lids 题库列表
     * @param n    题目数量
     * @return
     */
    public List<Question> findQuestionByHand(int[] lids, int n, int type) {
        try {
            // 查询某几个题库的所有类型的题目
            List<Question> questions = questionDao.findQuestionByLidList(lids, type);
            List<Question> newQuestion = new ArrayList<Question>();
            Map<Integer, String> map = new HashMap<Integer, String>();
            if (questions.size() <= n) {
                return questions;
            } else {
                while (map.size() < n) {
                    int random = (int) (Math.random() * questions.size());
                    // 判断Key是否存在
                    if (!map.containsKey(random)) {
                        map.put(random, "");
                        newQuestion.add(questions.get(random));
                    }
                }
            }
            return newQuestion;
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    // 查询试卷详情
    @Override
    public ResponseData findPaperDetails(int paperId) {
        try {
            List<Integer> paperlList = paperDao.findPaperDetails(paperId);
            // 获取到试卷所有题目的id；
            System.out.println(paperlList);
            List<Question> questions = new ArrayList<>();
            for (Integer integer : paperlList) {
                if(questionDao.findQuestionByQid(integer) != null){
                    questions.add(questionDao.findQuestionByQid(integer));
                }
            }
            List<Question> single = new ArrayList<>();
            List<Question> multiple = new ArrayList<>();
            List<Question> estimate = new ArrayList<>();
            List<Question> gap = new ArrayList<>();
            List<Question> essay = new ArrayList<>();
            for (Question question : questions) {
                if (question.getType() == 1) {
                    single.add(question);
                }
                if (question.getType() == 2) {
                    multiple.add(question);
                }
                if (question.getType() == 3) {
                    estimate.add(question);
                }
                if (question.getType() == 4) {
                    gap.add(question);
                }
                if (question.getType() == 5) {
                    essay.add(question);
                }
            }
            Map<String, Object> map = new HashMap<>();
            map.put("single", single);
            map.put("multip", multiple);
            map.put("estimate", estimate);
            map.put("gap", gap);
            map.put("essay", essay);
            return new ResponseData("0", "请求成功", map);
        } catch (Exception e) {
            System.out.println(e);
            return new ResponseData("888", "请求失败");
        }
    }

    // 查询试卷列表
    @Override
    public ResponseData findPaperList() {
        try {
            List<Integer> papers = paperDao.findAllPaperList();
            return new ResponseData("0", "请求成功", papers);
        } catch (Exception e) {
            System.out.println(e);
            return new ResponseData("888", "请求失败");
        }
    }

}
